//**************************************************************** 
//* Copyright (c) 2008 Ford Motor Company. All Rights Reserved. 
//* 
//* $$Workfile:   EconomicIndexStatusDao.java  $$ 
//* $$Revision:   1.2  $$ 
//* $$Author:   TMALDON4  $$ 
//* $$Date:   Feb 06 2009 05:32:04  $$ 
//* 
//*****************************************************************
package ar.com.ford.it.fullEconomics.persistence.dao;

import java.sql.Timestamp;
import java.util.Collection;
import java.util.HashMap;
import ar.com.ford.it.fullEconomics.persistence.dto.Teese01Economic;
import ar.com.ford.it.fullEconomics.persistence.dto.Teese03Econvalue;
import ar.com.ford.it.webengine.persistence.PersistenceUtil;
import com.ford.it.logging.ILogger;
import com.ford.it.logging.LogFactory;
import com.ford.it.persistence.PersistenceException;
import com.ford.it.persistence.PersistenceManager;
import com.ford.it.persistence.Query;

public class EconomicIndexStatusDao extends Dao {

    private static final String CLASS_NAME = EconomicIndexStatusDao.class
            .getName();
    private static final ILogger log = LogFactory.getInstance().getLogger(
            CLASS_NAME);

    /**
     * Busca los Economic Values actualizados en el periodo (minDate, maxDate)
     * para el Economic "economic"
     * 
     * @param economic
     * @param minDate
     * @param maxDate
     * @return
     * @throws PersistenceException
     */
    public Collection<Teese03Econvalue> findEconomicIndexValuesUpdated(
                                                                       Teese01Economic economic,
                                                                       Timestamp minDate,
                                                                       Timestamp maxDate,
                                                                       String actualOrForecast)
            throws PersistenceException {

        String METHOD_NAME = "findEconomicIndexValuesUpdated";
        log.entering(CLASS_NAME, METHOD_NAME);

        Collection<Teese03Econvalue> resultCol = null;
        PersistenceManager pm = null;
        Object[] parameters = { economic, minDate, maxDate, actualOrForecast };

        pm = PersistenceUtil.getPersistenceManager();
        Query query = pm.newQuery("findEconomicIndexValuesUpdated",
                Teese03Econvalue.class);
        query.setParameterArray(parameters);

        resultCol = (Collection) pm.executeQuery(query);

        log.exiting(CLASS_NAME, METHOD_NAME);
        return resultCol;
    }

    /**
     * Busca los Economic Values no actualizados con respecto a la fecha
     * targetDate pasada por parametro
     * 
     * @param economic
     * @param targetDate
     * @return
     * @throws PersistenceException
     */
    public Collection<Teese03Econvalue> findEconomicIndexValuesNotUpdated(
                                                                          Teese01Economic economic,
                                                                          Timestamp targetDate)
            throws PersistenceException {

        String METHOD_NAME = "findEconomicIndexNotValuesUpdated";
        log.entering(CLASS_NAME, METHOD_NAME);

        Collection<Teese03Econvalue> resultCol = null;
        PersistenceManager pm = null;

        pm = PersistenceUtil.getPersistenceManager();
        Query query = pm.newQuery("findEconomicIndexValuesNotUpdated",
                Teese03Econvalue.class);
        query.setParameters(economic, targetDate);

        resultCol = (Collection) pm.executeQuery(query);

        log.exiting(CLASS_NAME, METHOD_NAME);
        return resultCol;
    }

    /**
     * Busca el ultimo economic index value registrado para el economic pasado
     * por parametro
     * 
     * @param economic
     * @param minEffDate
     * @return
     * @throws PersistenceException
     */
    public Teese03Econvalue findFirstEconomicIndexValue(Teese01Economic economic,String type)
            throws PersistenceException {

        String METHOD_NAME = "findFirstEconomicIndexValue";
        log.entering(CLASS_NAME, METHOD_NAME);

        Teese03Econvalue result = null;
        PersistenceManager pm = null;

        pm = PersistenceUtil.getPersistenceManager();
        Query query = pm.newQuery("findFirstEconomicIndexValue",
                Teese03Econvalue.class);
        query.setParameters(economic, type);

        result = (Teese03Econvalue) pm.executeQuery(query);

        log.exiting(CLASS_NAME, METHOD_NAME);
        return result;
    }

    /**
     * Devuelve el menor effectiveDate asociado a los Economic Values asociados
     * a "economic"
     * 
     * @param economic
     * @return
     * @throws PersistenceException
     */
    public Timestamp findMinEconomicIndexValueEffDate(Teese01Economic economic)
            throws PersistenceException {
        String METHOD_NAME = "findMinEconomicIndexValueEffDate";
        log.entering(CLASS_NAME, METHOD_NAME);

        Timestamp result = null;
        PersistenceManager pm = null;
        HashMap<String, Timestamp> aux;

        pm = PersistenceUtil.getPersistenceManager();
        Query query = pm.newQuery("findMinEconomicIndexValueEffDate",
                Teese03Econvalue.class);
        query.setParameters(economic);

        aux = (HashMap<String, Timestamp>) pm.executeQuery(query);

        result = aux.get("minEffectiveDate");

        log.exiting(CLASS_NAME, METHOD_NAME);
        return result;
    }

}
